package com.bank.service.impl;

import com.bank.common.JsonResult;
import com.bank.dao.UserDao;
import com.bank.entity.User;
import com.bank.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: bankSystem
 * @Author: Jsx
 * @Time: 2020/9/12 16:57
 * @Description:
 */

@Service//相当于new了一下UserServiceImpl
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(String bankCode, String password) {
        User user = userDao.findByCode(bankCode);
        if (user == null) {
            throw new RuntimeException("账户错误");
        }
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }
        return user;
    }

    @Override
    public void updateHeadImg(Integer id, String imgPath) {
        User user = new User();
        user.setId(id);
        user.setImgPath(imgPath);
        System.out.println("user = " + user);
        userDao.update(user);
    }

    @Override
    public User selectByCode(String bankCode) {
        return userDao.findByCode(bankCode);
    }

    @Override
    public void updatePassword(String bankCode, String oldPassword, String newPassword) {
        //获取用户信息
        User user = userDao.findByCode(bankCode);

        //进行密码比较
        if (!user.getPassword().equals(oldPassword)){
            System.out.println(oldPassword);
            throw  new RuntimeException("原密码错误,请检查后再试");
        }

        User u = new User();
        u.setId(user.getId());
        u.setPassword(newPassword);
        userDao.update(u);
    }


    @Override
    public JsonResult inserUser(User user) {
        User nuser = userDao.findByCode(user.getBankCode());

        if (nuser != null) {
            return new JsonResult(0,"注册失败,该账户已经存在");
        }
        userDao.insertUser(user);
        return new JsonResult(1,"注册成功");

    }
}
